<?php

class AppInfo
{
    private $root;
    /**
     * @var string
     */
    private $appName = "";
    /**
     * @var string
     */
    private $appVersion = "";
    /**
     * @var string
     */
    private $appUpdateLink = "";
    /**
     * @var string
     */
    private $appInfoP = "";
    /**
     * @var array|mixed
     */
    private $appInfoC = array();
    /**
     * @var array
     */
    private $appNotices = array();
    public function __construct()
    {
        $this->root = $_SERVER['DOCUMENT_ROOT'] . "/php/";
        $this->appInfoP = "$this->root/data/app_info.json";
        if (!file_exists($this->appInfoP)){
            file_put_contents($this->appInfoP,json_encode(array()));
        } else {
            $this->appInfoC = json_decode(file_get_contents($this->appInfoP),true);
            $this->appNotices = json_decode($this->appInfoC["notices"]);
        }
    }

    /**
     * @return string
     */
    public function getAppName(): string
    {
        return $this->appInfoC["name"];
    }

    /**
     * @param string $appName
     */
    public function setAppName(string $appName)
    {
        $this->appName = $appName;
    }

    /**
     * @return string
     */
    public function getAppVersion(): string
    {
        return $this->appInfoC["version"];
    }

    /**
     * @param string $appVersion
     */
    public function setAppVersion(string $appVersion)
    {
        $this->appVersion = $appVersion;
    }

    /**
     * @return string
     */
    public function getAppUpdateLink(): string
    {
        return $this->appInfoC["app_update_link"];
    }

    /**
     * @param string $appUpdateLink
     */
    public function setAppUpdateLink(string $appUpdateLink)
    {
        $this->appUpdateLink = $appUpdateLink;
    }

    /**
     * @return array
     */
    public function getAppNotices()
    {
        return $this->appNotices;
    }

    public function addAppNotice($content, $tag)
    {
        $this->appNotices[] = array($content, $tag);
    }

    public function removeAppNotice($nid)
    {
        if (isset($this->appNotices[$nid])) {
            unset($this->appNotices[$nid]);
            $this->appNotices = array_values($this->appNotices);
        } else {
            die("删除成功");
        }
    }

    public function build()
    {
        $this->appInfoC["name"] = $this->appName;
        $this->appInfoC["version"] = $this->appVersion;
        $this->appInfoC["app_update_link"] = $this->appUpdateLink;
        $this->appInfoC["notices"] = json_encode($this->appNotices);
        return file_put_contents($this->appInfoP, json_encode($this->appInfoC));
    }
}

/*
$root = $_SERVER["DOCUMENT_ROOT"];
include_once "$root/class/SqlManager.php";
include_once "$root/class/SqlConnect.php";

$sqlConnect = new SqlConnect();
$sqlManager = new SqlManager("");
$sqlManager->setSqlConnect($sqlConnect->connect());
$this->sqlManager = $sqlManager;
 */